import { cloudDBZone, initCloudDB } from '@/AGC/manager'
import { CloudDBZoneQuery } from '@hw-agconnect/database'
import { UserFilters } from '../model/UserFilters'

/**
 * 执行插入--通用方法
 * @param model 模型
 */
export const normalExecuteUpsert = async <T>(model: T) => {
  if (!cloudDBZone) await initCloudDB()

  const result = await cloudDBZone.executeUpsert(model)
  console.log('normal-execute-upsert: ' + result)
}

/**
 * 查询用户筛选条件
 * @param user_id 用户ID
 * @returns 用户筛选条件
 */
export const qeuryUserFilters = async (user_id: number) => {
  if (!cloudDBZone) await initCloudDB()

  const query = await CloudDBZoneQuery.where(UserFilters)
  query.equalTo('user_id', user_id)
  const snapshot = await cloudDBZone.executeQuery(query)
  return snapshot.getSnapshotObjects()
}

/**
 * 执行用户筛选条件删除
 * @param userFilters 用户筛选条件
 */
export const executeDeleteUserFilters = async (userFilters: UserFilters) => {
  if (!cloudDBZone) await initCloudDB()

  const cloudDBResult = await cloudDBZone.executeDelete(userFilters)
  console.log('userFilters-delete: ' + cloudDBResult)
}
